package com.demo.web;

import com.demo.dto.request.URLIndexRequest;
import com.demo.dto.request.URLUpdateRequest;
import com.demo.service.SearchBaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Administrator on 2016/4/26.
 */
@Controller
@Api(tags = "接口", description = "接口说明")
@RequestMapping(value = "/demo")
public class SearchBaseController {

    @Autowired
    SearchBaseService searchBaseService;

    @ApiOperation(value = "URL索引")
    @RequestMapping(value = "/url", method = RequestMethod.POST)
    public ResponseEntity URLIndex(
            @ApiParam(value = "请求体", required = true) @RequestBody URLIndexRequest request
    ) {
        return ResponseEntity.ok(searchBaseService.URLIndex(request.getUrl()));
    }

    @ApiOperation(value = "URL获取")
    @RequestMapping(value = "/url/{id}", method = RequestMethod.GET)
    public ResponseEntity URLGet(
            @ApiParam(value = "编号", required = true) @PathVariable("id") String id) {
        return ResponseEntity.ok(searchBaseService.URLGet(id));
    }

    @ApiOperation(value = "URL列表")
    @RequestMapping(value = "/urls", method = RequestMethod.GET)
    public ResponseEntity URLGets() {
        return ResponseEntity.ok(searchBaseService.URLGets());
    }

    @ApiOperation(value = "URL删除")
    @RequestMapping(value = "/url/{id}", method = RequestMethod.DELETE)
    public ResponseEntity URLDelete(
            @ApiParam(value = "编号", required = true) @PathVariable("id") String id) {
        searchBaseService.URLDelete(id);
        return ResponseEntity.ok().build();
    }

    @ApiOperation(value = "URL更新")
    @RequestMapping(value = "/url", method = RequestMethod.PUT)
    public ResponseEntity URLUpdate(
            @ApiParam(value = "请求体", required = true) @RequestBody URLUpdateRequest request) {
        searchBaseService.URLUpdate(request.getUrl());
        return ResponseEntity.ok().build();
    }

}
